﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicalLog.Domain.Entities;
using MedicalLog.Domain.Repositories;

namespace MedicalLog.WebUI.Controllers
{
    public class EmergencyFormController : Controller
    {
        EmergencyFormRepository EFrepo;
        
        public EmergencyFormController()
        {
            EFrepo = new EmergencyFormRepository();
        }

        #region ViewModels
        public class VitalSignsViewModel
        {
            public DateTime Time { get; set; }
            public VitalSing TA { get; set; }
            public VitalSing FC { get; set; }
            public VitalSing FR { get; set; }
            public VitalSing Temp { get; set; }
            public VitalSing SatO2 { get; set; }
            public VitalSing Glicemia { get; set; }
        }

        public class EmergencyFormViewModel
        {
            public Patient patient {get; set;}
            public EmergencyForm emergencyForm {get; set;}
            public Doctor doctor { get; set; }
            public LabTest blanco { get; set; }
            public LabTest Hplus { get; set; }
            public LabTest Hg { get; set; }
            public LabTest Plaquetas { get; set; }
            public LabTest Sodio { get; set; }
            public LabTest Potasio { get; set; }
            public LabTest Cloro { get; set; }
            public LabTest Bicarbonato { get; set; }
            public LabTest Bun { get; set; }
            public LabTest Crent { get; set; }
            public LabTest Glucemia { get; set; }
            public LabTest Troponimia { get; set; }
            public LabTest CPK { get; set; }
            public LabTest CKMB { get; set; }
            public LabTest Urinalisis { get; set; }
            public LabTest Otros { get; set; }
            public List<VitalSignsViewModel> VitalSigns { get; set; }
        }
        #endregion
        //
        // GET: /EmergencyForm/

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult AddEmergencyForm(int pPatientId)
        {
            List<SelectListItem> Doctors = new List<SelectListItem>();
            DoctorRepository repo = new DoctorRepository();

            EmergencyFormViewModel EM = new EmergencyFormViewModel();
            EM.patient = getPatient(pPatientId);
            EM.VitalSigns = new List<VitalSignsViewModel>();
            EM.VitalSigns.Add(new VitalSignsViewModel());

            foreach (Doctor d in repo.getDoctors())
            {
                Doctors.Add(new SelectListItem
                {
                    Text = d.Name + " " + d.LastName,
                    Value = d.IdDoctor.ToString()
                });
            }

            ViewData["DoctorInformation"] = Doctors.AsEnumerable();
            return View(EM);
        }

        [HttpPost]
        public ActionResult AddEmergencyForm(EmergencyFormViewModel EMF)
        {
            DateTime currentDate = DateTime.Now;

            EMF.emergencyForm.Date = currentDate;
            EMF.emergencyForm.Doctor = EFrepo.getDoctor(EMF.doctor.IdDoctor);
            EMF.emergencyForm.Patient = EFrepo.getPatient(EMF.patient.Id);
            
            EFrepo.AddEmergencyForm(EMF.emergencyForm);

            EMF.Bicarbonato.Date = currentDate;
            EMF.Bicarbonato.TestList = EFrepo.getTestList("BICARBONATO");
            EMF.Bicarbonato.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Bicarbonato);

            EMF.blanco.Date = currentDate;
            EMF.blanco.TestList = EFrepo.getTestList("BLANCO");
            EMF.blanco.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.blanco);

            EMF.Bun.Date = currentDate;
            EMF.Bun.TestList = EFrepo.getTestList("BUN");
            EMF.Bun.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Bun);

            EMF.CKMB.Date = currentDate;
            EMF.CKMB.TestList = EFrepo.getTestList("CKMB");
            EMF.CKMB.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.CKMB);

            EMF.Cloro.Date = currentDate;
            EMF.Cloro.TestList = EFrepo.getTestList("CLORO");
            EMF.Cloro.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Cloro);

            EMF.CPK.Date = currentDate;
            EMF.CPK.TestList = EFrepo.getTestList("CPK");
            EMF.CPK.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.CPK);

            EMF.Crent.Date = currentDate;
            EMF.Crent.TestList = EFrepo.getTestList("CRENT");
            EMF.Crent.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Crent);

            EMF.Glucemia.Date = currentDate;
            EMF.Glucemia.TestList = EFrepo.getTestList("GLUCEMIA");
            EMF.Glucemia.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Glucemia);

            EMF.Hg.Date = currentDate;
            EMF.Hg.TestList = EFrepo.getTestList("HG");
            EMF.Hg.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Hg);

            EMF.Hplus.Date = currentDate;
            EMF.Hplus.TestList = EFrepo.getTestList("HPLUS");
            EMF.Hplus.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Hplus);

            EMF.Otros.Date = currentDate;
            EMF.Otros.TestList = EFrepo.getTestList("OTROS");
            EMF.Otros.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Otros);

            EMF.Plaquetas.Date = currentDate;
            EMF.Plaquetas.TestList = EFrepo.getTestList("PLAQUETAS");
            EMF.Plaquetas.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Plaquetas);

            EMF.Potasio.Date = currentDate;
            EMF.Potasio.TestList = EFrepo.getTestList("POTASIO");
            EMF.Potasio.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Potasio);

            EMF.Sodio.Date = currentDate;
            EMF.Sodio.TestList = EFrepo.getTestList("SODIO");
            EMF.Sodio.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Sodio);

            EMF.Troponimia.Date = currentDate;
            EMF.Troponimia.TestList = EFrepo.getTestList("TROPONIMIA");
            EMF.Troponimia.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Troponimia);

            EMF.Urinalisis.Date = currentDate;
            EMF.Urinalisis.TestList = EFrepo.getTestList("URINALISIS");
            EMF.Urinalisis.EmergencyForm = EMF.emergencyForm;
            EFrepo.AddLabTest(EMF.Urinalisis);

            foreach (VitalSignsViewModel item in EMF.VitalSigns)
            {
                item.FC.Date = item.Time;
                item.FC.VitalSingList = EFrepo.getVitalSignList("FC");
                item.FC.EmergencyForm = EMF.emergencyForm;
                EFrepo.AddVitalSign(item.FC);
                item.FR.Date = item.Time;
                item.FR.VitalSingList = EFrepo.getVitalSignList("FR");
                item.FR.EmergencyForm = EMF.emergencyForm;
                EFrepo.AddVitalSign(item.FR);
                item.Glicemia.Date = item.Time;
                item.Glicemia.VitalSingList = EFrepo.getVitalSignList("GLICEMIA");
                item.Glicemia.EmergencyForm = EMF.emergencyForm;
                EFrepo.AddVitalSign(item.Glicemia);
                item.SatO2.Date = item.Time;
                item.SatO2.VitalSingList = EFrepo.getVitalSignList("SATO2");
                item.SatO2.EmergencyForm = EMF.emergencyForm;
                EFrepo.AddVitalSign(item.SatO2);
                item.TA.Date = item.Time;
                item.TA.VitalSingList = EFrepo.getVitalSignList("TA");
                item.TA.EmergencyForm = EMF.emergencyForm;
                EFrepo.AddVitalSign(item.TA);
                item.Temp.Date = item.Time;
                item.Temp.VitalSingList = EFrepo.getVitalSignList("TEMP");
                item.Temp.EmergencyForm = EMF.emergencyForm;
                EFrepo.AddVitalSign(item.Temp);
            }

            return RedirectToAction("SearchPatient", "Patient");
        }

        [HttpGet]
        public ActionResult EditEmergencyForm(int EmergencyFormId)
        {
            List<SelectListItem> Doctors = new List<SelectListItem>();
            EmergencyFormViewModel EMF = new EmergencyFormViewModel();
            List<VitalSignsViewModel> VSVM = new List<VitalSignsViewModel>();

            EMF.emergencyForm = EFrepo.DetailEmergencyForm(EmergencyFormId);
            DoctorRepository repo = new DoctorRepository();

            foreach (Doctor d in repo.getDoctors())
            {
                Doctors.Add(new SelectListItem
                {
                    Text = d.Name + " " + d.LastName,
                    Value = d.IdDoctor.ToString()
                });
            }

            ViewData["DoctorInformation"] = Doctors.AsEnumerable();

            EMF.patient = EMF.emergencyForm.Patient;
            EMF.doctor = EMF.emergencyForm.Doctor;

            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "BICARBONATO").Count() > 0)
                EMF.Bicarbonato = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "BICARBONATO").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "BLANCO").Count() > 0)
                EMF.blanco = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "BLANCO").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "BUN").Count() > 0)
                EMF.Bun = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "BUN").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "CKMB").Count() > 0)
                EMF.CKMB = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "CKMB").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "CLORO").Count() > 0)
                EMF.Cloro = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "CLORO").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "CPK").Count() > 0)
                EMF.CPK = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "CPK").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "CRENT").Count() > 0)
                EMF.Crent = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "CRENT").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "GLUCEMIA").Count() > 0)
                EMF.Glucemia = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "GLUCEMIA").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "HG").Count() > 0)
                EMF.Hg = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "HG").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "HPLUS").Count() > 0)
                EMF.Hplus = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "HPLUS").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "OTROS").Count() > 0)
                EMF.Otros = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "OTROS").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "PLAQUETAS").Count() > 0)
                EMF.Plaquetas = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "PLAQUETAS").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "POTASIO").Count() > 0)
                EMF.Potasio = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "POTASIO").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "SODIO").Count() > 0)
                EMF.Sodio = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "SODIO").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "TROPONIMIA").Count() > 0)
                EMF.Troponimia = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "TROPONIMIA").First();
            if (EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "URINALISIS").Count() > 0)
                EMF.Urinalisis = EMF.emergencyForm.LabTests.Where(x => x.TestList.TestName == "URINALISIS").First();

            VitalSignsViewModel VSVMSingle = new VitalSignsViewModel();
            foreach (VitalSing item in EMF.emergencyForm.VitalSings.OrderBy(x => x.Id).Select(x => x))
            {
                VSVMSingle.Time = item.Date.Value;
                if (item.VitalSingList.VitalSignName == "FC")
                {
                    if (VSVMSingle.FC == null)
                        VSVMSingle.FC = item;
                    else
                    {
                        VSVM.Add(VSVMSingle);
                        VSVMSingle = new VitalSignsViewModel();
                        VSVMSingle.FC = item;
                    }
                }
                if (item.VitalSingList.VitalSignName == "FR")
                {
                    if (VSVMSingle.FR == null)
                        VSVMSingle.FR = item;
                    else
                    {
                        VSVM.Add(VSVMSingle);
                        VSVMSingle = new VitalSignsViewModel();
                        VSVMSingle.FR = item;
                    }
                }
                if (item.VitalSingList.VitalSignName == "GLICEMIA")
                {
                    if (VSVMSingle.Glicemia == null)
                        VSVMSingle.Glicemia = item;
                    else
                    {
                        VSVM.Add(VSVMSingle);
                        VSVMSingle = new VitalSignsViewModel();
                        VSVMSingle.Glicemia = item;
                    }
                }
                if (item.VitalSingList.VitalSignName == "SATO2")
                {
                    if (VSVMSingle.SatO2 == null)
                        VSVMSingle.SatO2 = item;
                    else
                    {
                        VSVM.Add(VSVMSingle);
                        VSVMSingle = new VitalSignsViewModel();
                        VSVMSingle.SatO2 = item;
                    }
                }
                if (item.VitalSingList.VitalSignName == "TA")
                {
                    if (VSVMSingle.TA == null)
                        VSVMSingle.TA = item;
                    else
                    {
                        VSVM.Add(VSVMSingle);
                        VSVMSingle = new VitalSignsViewModel();
                        VSVMSingle.TA = item;
                    }
                }
                if (item.VitalSingList.VitalSignName == "TEMP")
                {
                    if (VSVMSingle.Temp == null)
                        VSVMSingle.Temp = item;
                    else
                    {
                        VSVM.Add(VSVMSingle);
                        VSVMSingle = new VitalSignsViewModel();
                        VSVMSingle.Temp = item;
                    }
                }
            }

            EMF.VitalSigns = VSVM;

            return View(EMF);
        }

        [HttpPost]
        public ActionResult EditEmergencyForm(EmergencyFormViewModel EMF)
        {
            DateTime currentDate = EMF.emergencyForm.Date;

            EFrepo.UpdateEmergencyForm(EMF.emergencyForm);

            EMF.Bicarbonato.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Bicarbonato, EMF.emergencyForm, "BICARBONATO");

            EMF.blanco.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.blanco, EMF.emergencyForm, "BLANCO");

            EMF.Bun.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Bun, EMF.emergencyForm, "BUN");

            EMF.CKMB.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.CKMB, EMF.emergencyForm, "CKMB");

            EMF.Cloro.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Cloro, EMF.emergencyForm, "CLORO");

            EMF.CPK.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.CPK, EMF.emergencyForm, "CPK");

            EMF.Crent.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Crent, EMF.emergencyForm, "CRENT");

            EMF.Glucemia.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Glucemia, EMF.emergencyForm, "GLUCEMIA");

            EMF.Hg.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Hg, EMF.emergencyForm, "HG");

            EMF.Hplus.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Hplus, EMF.emergencyForm, "HPLUS");

            EMF.Otros.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Otros, EMF.emergencyForm, "OTROS");

            EMF.Plaquetas.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Plaquetas, EMF.emergencyForm, "PLAQUETAS");

            EMF.Potasio.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Potasio, EMF.emergencyForm, "POTASIO");

            EMF.Sodio.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Sodio, EMF.emergencyForm, "SODIO");

            EMF.Troponimia.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Troponimia, EMF.emergencyForm, "TROPONIMIA");

            EMF.Urinalisis.Date = currentDate;
            EFrepo.UpdateLabTest(EMF.Urinalisis, EMF.emergencyForm, "URINALISIS");

            if (EMF.VitalSigns != null)
            {
                foreach (VitalSignsViewModel item in EMF.VitalSigns)
                {
                    item.FC.Date = item.Time;
                    EFrepo.UpdateVitalSign(item.FC, EMF.emergencyForm, "FC");
                    item.FR.Date = item.Time;
                    EFrepo.UpdateVitalSign(item.FR, EMF.emergencyForm, "FR");
                    item.Glicemia.Date = item.Time;
                    EFrepo.UpdateVitalSign(item.Glicemia, EMF.emergencyForm, "GLICEMIA");
                    item.SatO2.Date = item.Time;
                    EFrepo.UpdateVitalSign(item.SatO2, EMF.emergencyForm, "SATO2");
                    item.TA.Date = item.Time;
                    EFrepo.UpdateVitalSign(item.TA, EMF.emergencyForm, "TA");
                    item.Temp.Date = item.Time;
                    EFrepo.UpdateVitalSign(item.Temp, EMF.emergencyForm, "TEMP");
                }
            }

            return RedirectToAction("SearchPatient", "Patient");
        }

        public ActionResult AddVitalSign()
        {
            return View("VitalSignsViewModel", new VitalSignsViewModel());
        }
        
        #region Reutilisable functions
        private Patient getPatient(int pPatientId)
        {
            PatientsRepository repo = new PatientsRepository();
            return repo.DetailPatient(pPatientId);
        }
        #endregion

        #region Ajax Calls
        public ActionResult getSuggestions(string pText)
        {
            var Result = EFrepo.getDiagnostic(pText);
            return View(Result.AsEnumerable());
        }

        public void DeleteRowVS(int TA, int FC, int FR, int Temp, int SatO2, int Glicemia)
        {
            VitalSignRepository VSrepo = new VitalSignRepository();

            VSrepo.DeleteVitalSign(TA);
            VSrepo.DeleteVitalSign(FC);
            VSrepo.DeleteVitalSign(FR);
            VSrepo.DeleteVitalSign(Temp);
            VSrepo.DeleteVitalSign(SatO2);
            VSrepo.DeleteVitalSign(Glicemia);
        }
        #endregion
    }
}